package com.ics.atable.chat.model.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.ics.atable.chat.serializer.ReplaceDeserializer;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class SqlReplaceDTO {
    
    @Data
    public static class RunRequest {
        @Schema(description = "替换对象，包含SQL语句和其他替换字段")
        private Replace replace;
        
        @Data
        @JsonDeserialize(using = ReplaceDeserializer.class)
        public static class Replace {
            // 明确定义数据库字段，替代replaceMap，与表结构保持一致
            // 所有字段在SQL替换时需要进行判空，如果SQL中含有该字段则需替换，没有则不输出
            @Schema(description = "主键")
            private String id;
            
            @Schema(description = "数据列唯一id")
            private String dataCellUuid;
            
            @Schema(description = "父级模板指标和模板表的关系id")
            private String parentItemTableRelationId;
            
            @Schema(description = "当前机构父数据表UUID")
            private String parentDataTableUuid;
            
            @Schema(description = "列值，选项全量以JSON格式存储，未选为0")
            private String cellValue;
            
            @Schema(description = "填报机构id")
            private String provideOrgId;
            
            @Schema(description = "填报机构级别（冗余）：1省，2市，3县，4镇，5村")
            private String provideAdminLevel;
            
            @Schema(description = "父列id")
            private String parentMouldColumnUuid;
            
            @Schema(description = "创建时间")
            private String createDate;
            
            @Schema(description = "修改时间")
            private String updateDate;
            
            @Schema(description = "备注")
            private String remark;
            
            @Schema(description = "每次提交清0，每一次保存都变1")
            private String submitFlag;
            
            @Schema(description = "上一次提交的值")
            private String hisCellValue;
            
            @Schema(description = "指标名称")
            private String itemId;
            
            @Schema(description = "指标名称，其中既存储了大类（如\"叶菜类\"、\"白菜类\"），也存储了子类（如\"西瓜\"），有时也存储具体品种（如\"小白菜\"、\"油麦菜\"）。")
            private String itemName;
            
            @Schema(description = "指标全量id")
            private String fullItemId;
            
            @Schema(description = "指标全量名称")
            private String fullItemName;
            
            @Schema(description = "列名称，只有俩个列名称，分别为本年和上年")
            private String columnName;
            
            @Schema(description = "填报列全量id")
            private String fullColumnId;
            
            @Schema(description = "填报列全量名称")
            private String fullColumnName;
            
            @Schema(description = "村级行政区划名称")
            private String villageOrgName;
            
            @Schema(description = "村级行政区划id")
            private String townOrgId;
            
            @Schema(description = "镇级行政区划名称")
            private String townOrgName;
            
            @Schema(description = "县级行政区划id")
            private String countOrgId;
            
            @Schema(description = "县级行政区划名称")
            private String countOrgName;
            
            @Schema(description = "市级行政区划id")
            private String cityOrgId;
            
            @Schema(description = "市级行政区划名称")
            private String cityOrgName;
            
            @Schema(description = "省级行政区划id")
            private String provinceOrgId;
            
            @Schema(description = "省级行政区划名称")
            private String provinceOrgName;
            
            @Schema(description = "完整区划名，用/链接")
            private String fullOrgName;
            
            @Schema(description = "任务名")
            private String taskName;
            
            @Schema(description = "报告期-年份")
            private String tableYear;
            
            @Schema(description = "报告期-季度，1代表第一季度，2代表第二季度，以此类推")
            private String taskQuarter;
            
            @Schema(description = "所属乡镇，code前9位")
            private String townCode;
            
            @Schema(description = "所属乡镇名称")
            private String townName;
            
            @Schema(description = "所属县，code前6位")
            private String countCode;
            
            @Schema(description = "所属县名称")
            private String countName;
            
            @Schema(description = "所属市，code前4位")
            private String cityCode;
            
            @Schema(description = "所属市名称")
            private String cityName;
            
            @Schema(description = "指标所属部门编码")
            private String deptCode;
            
            @Schema(description = "指标所属部门名称,暂时只有一个部门叫做统计部门-统计局农村处")
            private String deptName;
        }
    }
    
    @Data
    public static class ParseResult {
        @Schema(description = "解析是否成功")
        private boolean success;
        
        @Schema(description = "错误信息")
        private String errorMessage;
        
        @Schema(description = "解析后的SQL语句")
        private String sql;
    }
    
    @Data
    public static class ValidationResult {
        @Schema(description = "参数是否非空且有效")
        private boolean valid;
        
        @Schema(description = "参数为空或无效时的错误信息")
        private String errorMessage;
    }
    
    // 新增：查询请求 DTO
    @Data
    public static class QueryRequest {
        @Schema(description = "查询条件对象", required = true)
        @NotNull(message = "查询条件不能为空")
        @Valid
        private ReplaceParams replace;
        
        @Data
        public static class ReplaceParams {
            @Schema(description = "指标全量名称", required = true, example = "蔬菜（不含食用菌）/叶菜类/结球莴苣（包心生菜）")
            @NotBlank(message = "指标全量名称不能为空")
            private String fullItemName;
            
            @Schema(description = "填报列全量名称", required = true, example = "1-本季累计/产量/上年")
            @NotBlank(message = "填报列全量名称不能为空")
            private String fullColumnName;
            
            @Schema(description = "报告期-年份", required = true, example = "2024")
            @NotBlank(message = "年份不能为空")
            private String tableYear;
            
            @Schema(description = "报告期-季度", required = true, example = "1")
            @NotBlank(message = "季度不能为空")
            private String taskQuarter;
            
            @Schema(description = "完整区划名", required = true, example = "贵州省/贵阳市/息烽县")
            @NotBlank(message = "区划名不能为空")
            private String fullOrgName;
        }
    }
    
    // 新增：批量查询请求 DTO
    @Data
    public static class BatchQueryRequest {
        @Schema(description = "批量查询条件对象", required = true)
        @NotNull(message = "查询条件不能为空")
        @Valid
        private BatchReplaceParams replace;
        
        @Data
        public static class BatchReplaceParams {
            @Schema(description = "指标全量名称", required = true, example = "蔬菜（不含食用菌）/叶菜类/结球莴苣（包心生菜）")
            @NotBlank(message = "指标全量名称不能为空")
            private String fullItemName;
            
            @Schema(description = "填报列全量名称", required = true, example = "1-本季累计/产量/上年")
            @NotBlank(message = "填报列全量名称不能为空")
            private String fullColumnName;
            
            @Schema(description = "报告期-年份", required = true, example = "2024")
            @NotBlank(message = "年份不能为空")
            private String tableYear;
            
            @Schema(description = "报告期-季度", required = true, example = "1")
            @NotBlank(message = "季度不能为空")
            private String taskQuarter;
            
            @Schema(description = "完整区划名列表", required = true, example = "[\"贵州省/贵阳市/息烽县/永靖镇/上洪马村\", \"贵州省/贵阳市/息烽县/永靖镇/下洪马村\"]")
            @NotNull(message = "区划名列表不能为空")
            private List<String> fullOrgNames;
        }
    }
    
    // 新增：查询响应 DTO
    @Data
    public static class QueryResponse {
        @Schema(description = "查询结果列表")
        private List<DataCellWideResult> results;
        
        @Schema(description = "查询结果数量")
        private int count;
        
        @Schema(description = "自然语言解释")
        private String explanation;
        
        @Data
        public static class DataCellWideResult {
            @Schema(description = "指标全量名称")
            private String fullItemName;
            
            @Schema(description = "填报列全量名称")
            private String fullColumnName;
            
            @Schema(description = "报告期-年份")
            private String tableYear;
            
            @Schema(description = "报告期-季度")
            private String taskQuarter;
            
            @Schema(description = "完整区划名")
            private String fullOrgName;
            
            @Schema(description = "列值（包含单位）")
            private String cellValue;
        }
    }
}